package com.crm.controller.client;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.pojo.ClientTransfer;
import com.crm.pojo.Userinfo;
import com.crm.service.client.ClientInfoService;
import com.crm.service.client.ClientTransferService;
import com.crm.vo.Result;

@Controller
public class ClientTransferController {

	@Autowired
	public ClientInfoService clientInfoService;
	@Autowired
	public ClientTransferService clientTransferService;
	
	/**
	 * 跳转到客户转移页面
	 * @param model
	 * @return
	 */
	@GetMapping("/to-client-transfer")
	public String toClientTransfer(Model model) {
		List<Userinfo> list = clientInfoService.queryAllUser();
		model.addAttribute("users",list);
		return "client/client-transfer";
	}
	
	/**
	 * 跳转到客户转移结果页面
	 * @return
	 */
	@GetMapping("/to-client-transfer-result")
	public String toClientTransferTesult() {
		return "client/client-transfer-result";
	}
	
	/**
	 * 跳转到客户转移日志页面
	 * @return
	 */
	@GetMapping("/to-client-transfer-details")
	public String toClientTransferDetail() {
		return "client/client-transfer-details";
	}
	
	/**
	 * 进行客户转移，改变客户的拥有者，创建客户转移日志
	 * @param out_userId
	 * @param in_userId
	 * @param cid
	 * @param request
	 * @return
	 */
	@PostMapping("/transfer-client")
	@ResponseBody
	public Result transferClient(BigDecimal out_userId, BigDecimal in_userId, Long cid, HttpServletRequest request) {
		//获取操作人 即当前登录的用户
		HttpSession session = request.getSession();
		Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
		BigDecimal operateClientId = new BigDecimal(userinfo.getUId());
		return clientTransferService.transferClient(out_userId, in_userId, cid, operateClientId);
	}
	
	@PostMapping("/query-all-client-transfer")
	@ResponseBody
	public Map queryAllClientTransfer(int page, int rows){
		Long total = clientTransferService.queryCountAllClientTransfer();
		List<ClientTransfer> list = clientTransferService.queryAllClientTransfer(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@PostMapping("/query-client-transfer-by-inClientId")
	@ResponseBody
	public Map queryClientTransferByInClientId(BigDecimal inClientId, int page, int rows) {
		Long total = clientTransferService.queryCountClientTransferByInClientId(inClientId);
		List<ClientTransfer> list = clientTransferService.queryClientTransferByInClientId(inClientId, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@PostMapping("/query-client-transfer-by-outClientId")
	@ResponseBody
	public Map queryClientTransferByOutClientId(BigDecimal outClentId, int page, int rows) {
		Long total = clientTransferService.queryCountClientTransferByOutClientId(outClentId);
		List<ClientTransfer> list = clientTransferService.queryClientTransferByOutClientId(outClentId, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
}
